/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable","bch/app/vaccine/vaccineCardList.ui"],
    function($,$bb,$datatable,$ui){
	var servers = {};
	var URL = $bb.baseUrl("followapp");
    var hospitalName;

    /**
     * 请求发卡登记成功后的回调函数
     */
    var ajaxSaveVaccineInfoCallBack = function(data){
        $datatable.table.api().draw(false);
        var args = $datatable.table.api().ajax.params();   //获取dt请求参数
    }

    /**
     * 请求发卡登记
     */
    var ajaxSaveVaccineInfo = function(params,callback){
        $bb.ajax({
            url: URL + "ctrl/VaccineInfo/saveVaccineInfo",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "发卡登记出错！");
                }
            }
        });
    }

    /**
     * 提交发卡登记内容
     */
    var onSubmit = function(){
        var params = $(this).data("params");
        $("." + params.className).modal("hide");
        var inoculationCard = $("#inoculationCardModal").val();
        var sendCardTime = $("#sendCardTime").val();
        var cardIssuer = $("#cardIssuer").val();
        var channelId = $bb.getChannelId();
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var params1 = {
            "oprInfo":{
                "opCode":"1125",
                "opType":"2",
                "opNote":"【"+ doctorName +"】医生给【"+ params.userName +"】患者发疫苗接种卡",
                "channelType":"2",
                "channelId":channelId,
            },
            "queueId":params.queueId,
            "userId":params.userId,
            "sendCardState":"1",
            "sendCardTime":sendCardTime,
            "inoculationCard":inoculationCard,
            "cardIssuer":cardIssuer,
        };
        ajaxSaveVaccineInfo(params1,ajaxSaveVaccineInfoCallBack);
    }
    /**
     * 发卡登记
     */
    var onVaccineCard = function(){
        var params = $(this).data("params");
        var className = "vaccineCardModal";
        var time = $bb.simple.getCurTime().date;
        $bb.modal({
            "className": className,
            "tit": "疫苗接种发卡登记",
        },function(){
            $ui.vaccineCardWrap({
                "dom":$("." + className + " .modal-body"),
                "data":{
                    "time":time,
                    "hospital":hospitalName,
                }
            });
            $("." + className).modal("show");
            params.className = className;
            $("#submit").unbind("click");
            $("#submit").data("params",params);
            $("#submit").click(onSubmit);
        });
    }

    /************************ 以下是datatable ************************/
    
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "userName": $.trim($('#userName').val()),
                "parentName": $.trim($('#parentName').val()),
                "phoneNo": $.trim($('#phoneNo').val()),
                "inoculationCard": $.trim($('#inoculationCard').val()),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        hospitalName = data.hospitalName;
        return data.rows;
    }
    /**
     * columns
     */
    var columnsParentName = function(e){
        return e.motherName ? e.motherName : e.fatherName;
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":'inoculationCard'},
            {"data":'userName'},
            {"data":'phoneNo'},
            {"data":'birthday'},
            {"data":columnsParentName},
            {"data":'createTime'},
            {"data":'cardIssuer'},
            {"data":'sendCardTime'},
            {
                "data":null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = data.sendCardState == "1" ? '已发卡':'<div class="btn-solid btn-solid-green no-vaccineCard">未发卡</div>';
        $('td.operate', row).html(h);
        $("td.operate .no-vaccineCard",row).unbind("click");
        $("td.operate .no-vaccineCard",row).data("params",data);
        $("td.operate .no-vaccineCard",row).click(onVaccineCard);
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        $("#serchBtn").bind("click",function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/VaccineInfo/selectVaccineInfo?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});